//
//  CPNewDetaieController.m
//  News
//
//  Created by chen on 15/3/25.
//  Copyright (c) 2015年 chen. All rights reserved.
//

#import "CPNewDetaieController.h"
#import "CPNetManager.h"
#import "CPNewsDetailImage.h"

#import "CPNewsDetail.h"
@interface CPNewDetaieController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic, strong)  CPNewsDetail *detail;
@end

@implementation CPNewDetaieController

- (void)setDetail:(CPNewsDetail *)detail
{
    _detail = detail;
    [self showNewsDetail];

}

+ (CPNewDetaieController *)controller{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"NewDetail" bundle:nil];

    return  [sb instantiateInitialViewController];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setVc];
   
    [self sendRequest];
    
}

- (void)setVc{
    self.webView.delegate = self;
    self.title = @"新闻";
    
}


- (void)sendRequest{
    NSString *url = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/%@/full.html", self.NewsModal.docid];
    
    
    CPNetwork *net = [[CPNetwork alloc] init];
    [net requestJSON:url httpMethod:HTTPMethodGet parameter:nil completionHandler:^(NSData *JSONData, NSError *error) {
        
       NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil];
        
        NSDictionary *dic2 = dic[self.NewsModal.docid];
       
        self.detail = [CPNewsDetail detailWithDict:dic2];
        
        
    }];
 
    
    
}

- (void)showNewsDetail
{
    NSMutableString *html = [NSMutableString string];
    // 头部内容
    [html appendString:@"<html>"];
    [html appendString:@"<head>"];
    [html appendFormat:@"<link rel=\"stylesheet\" href=\"%@\">", [[NSBundle mainBundle] URLForResource:@"CPNewsDetail.css" withExtension:nil]];
    [html appendString:@"</head>"];
    
    // 具体内容
    [html appendString:@"<body>"];
    
    // 将图片插入body对应的标记中, 比如<!--IMG#0-->
    [html appendString:[self setupBody]];
    
    [html appendString:@"</body>"];
    
    // 尾部内容
    [html appendString:@"</html>"];
    
    // 显示网页
    [self.webView loadHTMLString:html baseURL:nil];
}

/**
 *  初始化body内容
 */
- (NSString *)setupBody
{
    NSMutableString *body = [NSMutableString string];
    
    // 拼接标题
    [body appendFormat:@"<div class=\"title\">%@</div>", self.detail.title];
    
    // 拼接时间
    [body appendFormat:@"<div class=\"time\">%@</div>", self.detail.ptime];
    
    // 拼接图片
    [body appendString:self.detail.body];
    for (CPNewsDetailImage *img in self.detail.img) {
        // 图片的html字符串
        NSMutableString *imgHtml = [NSMutableString string];
        [imgHtml appendString:@"<div class=\"img-parent\">"];
        
        // img.pixel = 500*332
        NSArray *pixel = [img.pixel componentsSeparatedByString:@"*"];
        int width = [[pixel firstObject] intValue];
        int height = [[pixel lastObject] intValue];
        int maxWidth = [UIScreen mainScreen].bounds.size.width * 0.8;
        if (width > maxWidth) { // 限制尺寸
            height = height * maxWidth / width;
            width = maxWidth;
        }
        
               
        [imgHtml appendFormat:@"<img  width=\"%d\" height=\"%d\" src=\"%@\">",  width, height, img.src];
        [imgHtml appendString:@"</div>"];
        
        // 将img.ref替换为img标签的内容
        [body replaceOccurrencesOfString:img.ref withString:imgHtml options:NSCaseInsensitiveSearch range:NSMakeRange(0, body.length)];
    }
    return body;
}




@end
